<?php


namespace logic\cyf;


use frontend\models\cyf\CommendGoods;
use frontend\models\cyf\Discussion;
use frontend\models\cyf\Goods;
use servers\cyf\EsServer;
use yii\db\Query;

class GoodsLogic
{
    /**
     * 商品列表
     * @param $params
     * @return array
     * @cyf
     */
    public static function index($params)
    {
        $offset =ceil($params['pag'] -1) * $params['length'];//偏移量
        $limit = $params['limit'];//每页显示条数
        $data =Goods::index($offset,$limit);//查询数据
        if(!$data) throw new \Exception('操作失败');
        return $data;
    }

    /**
     * 轮播图
     * @name cyf
     */
    public static function banner()
    {
        $banner = Goods::getBanner();
        if(!$banner){
            return  false;
        }
        return $banner;
    }

    /**
     * 推荐商品
     * @return array
     * @name cyf
     */
    public static function type()
    {
        $data = CommendGoods::getTypeID();
        if(!$data){
           return  false;
        }
        return  $data;
    }

    /**
     * 查询推荐类商品详情
     * @param $params
     * @return array
     * @name cyf
     */
    public static function commendGoods($params)
    {
            $goods_id_Arr = CommendGoods::getGoodsIDBytypeId($params);//获取商品id
            $offset =ceil($params['pag'] -1) * $params['length'];//偏移量
            $length = $params['length'];//每页显示条数
            $goods_id = [];//定义商品id数组
            foreach ($goods_id_Arr as $key=>$val){
                array_push($goods_id,$val['goods_id']);
            }
            $data = Goods::getGoodsByCommendId($goods_id,$offset,$length);//查询推荐类商品
            return $data;
    }

    /**
     * 商品详情
     * @param $goods_id
     * @return array|bool|\yii\db\ActiveRecord[]
     * @cyf
     */
    public static function goodsDetail($goods_id)
    {
        $goodsDetail = Goods::goodsDetail($goods_id);
        if(!$goodsDetail) return false;
        return  $goodsDetail;
    }



}